package com.timberwood.model.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.timberwood.model.entity.Users;
import com.timberwood.util.HibernateUtil;

public class UserDAO {
	
	private Session session;
	private Transaction transaction;
	
	public UserDAO() {
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		transaction = session.beginTransaction();
	}
	
	//method lấy về các User
	@SuppressWarnings("unchecked")
	public List<Users> sleAllUser(){
		List<Users> listUser=null;
		try {
			listUser=(List<Users>)session.getNamedQuery("sleAllUser").list();
		} catch (HibernateException he) {
			transaction.rollback();
			he.printStackTrace();
		}
		transaction.commit();
		return listUser;
	}
	
	//method lấy về User với UserId
	public Users sleUserById(int userId) {
		Users objuser=null;
		try {
			objuser=(Users)session.getNamedQuery("sleUserById").setInteger("userId", userId).uniqueResult();
		} catch (HibernateException he) {
			transaction.rollback();
			he.printStackTrace();
		}
		transaction.commit();
		return objuser;
	}
	
	
	//method update một user
	public boolean updateUser(Users objUser) {
		boolean status;
		try {
			session.update(objUser);
			status=true;
		} catch (HibernateException he) {
			status=false;
			transaction.rollback();
			he.printStackTrace();
		}
		transaction.commit();
		return status;
	}
	
	
	//method xóa một User
	public boolean deleteUser(int userId) {
		boolean status;
		try {
			session.getNamedQuery("deleteUser").setInteger("userId", userId).executeUpdate();
			status=true;
		} catch (HibernateException he) {
			status=false;
			transaction.rollback();
			he.printStackTrace();
		}
		transaction.commit();
		return status;
	}
	
	
	//method thêm mới một user
	public boolean insertUser(Users objUser) {
		boolean status;
		try {
			session.save(objUser);
			status=true;
		} catch (HibernateException he) {
			status=false;
			transaction.rollback();
			he.printStackTrace();
		}
		transaction.commit();
		return status;
	}
	
	public int checkLoginAdmin(String user,String pass){
		int status=0;
		try {
			status=session.getNamedQuery("checkLoginAdmin").setString("username", user).setString("password", pass).list().size();
		} catch (HibernateException he) {
			transaction.rollback();
			he.printStackTrace();
		}
		return status;
	}
	
	//method lấy về User đang hoạt động
	@SuppressWarnings("unchecked")
	public List<Users> sleUserActivity() {
		List<Users> listUser=null;
		try {
			listUser=(List<Users>)session.getNamedQuery("sleUserActivity").list();
		} catch (HibernateException he) {
			transaction.rollback();
			he.printStackTrace();
		}
		transaction.commit();
		return listUser;
	}
	
	//method lấy về User với UserId
	@SuppressWarnings("unchecked")
	public List<Users> sleUserBlock() {
		List<Users> listUser=null;
		try {
			listUser=(List<Users>)session.getNamedQuery("sleUserBlock").list();
		} catch (HibernateException he) {
			transaction.rollback();
			he.printStackTrace();
		}
		transaction.commit();
		return listUser;
	}
	
	
	
	
		
	
	
	
	

}
;